﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;//文件存取
using System.Drawing;//画图基本功能
using System.Drawing.Drawing2D;//二维画图
using System.Drawing.Imaging;//高级功能

namespace AdverseEventReportSystem.Utils
{
    public class WebPaintPic
    {
        #region 鱼骨图

        /// <summary>
        /// 绘制鱼骨图
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public MemoryStream drawYG(string name, DataTable dt)
        {
            int Bwidth = 1520;
            int Bheight = 600;
            Bitmap img = new Bitmap(Bwidth, 700);//创建Bitmap对象
            Graphics g = Graphics.FromImage(img);//创建Graphics对象

            Pen Bp = new Pen(Color.Black);//定义黑色画笔
            Pen Rp = new Pen(Color.Red);//红色画笔
            Pen Sp = new Pen(Color.Blue);//蓝色

            AdjustableArrowCap aac;//定义箭头帽

            aac = new System.Drawing.Drawing2D.AdjustableArrowCap(4, 4);
            Sp.CustomStartCap = aac;//开始端箭头帽

            Font Bfont = new Font("微软雅黑", 15, FontStyle.Bold);//大标题字体
            Font font = new Font("微软雅黑", 8, FontStyle.Bold);//一般字
            Font Tfont = new Font("微软雅黑", 10, FontStyle.Bold);//较大字体

            g.DrawRectangle(new Pen(Color.White, img.Width), 0, 0, img.Width, img.Height);//矩形 底色

            //黑色过度型笔刷
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Black, Color.Black, 1.2F, true);
            //蓝色过度型笔刷
            LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);

            StringFormat format1 = new StringFormat();
            format1.Trimming = StringTrimming.EllipsisWord;//修改这里

            g.DrawString("鱼骨图", Bfont, brush, 40, 5);//绘制大标题

            double x1 = 1200;
            double y1 = Bheight / 2;
            double x2 = 100;
            double y2 = Bheight / 2;
            g.DrawLine(Sp, (float)x1, (float)y1, (float)x2, (float)y2);//总线
            //鱼头
            Rectangle rc = new Rectangle(1150, 250, 100, 100);
            Rectangle rc1 = new Rectangle(1120, 259, 82, 82);
            g.DrawArc(Bp, rc, 125, -250);
            g.DrawArc(Bp, rc1, 78, -156);
            g.DrawString(name, Bfont, brush, 1250, (float)y1 - 60, new StringFormat(StringFormatFlags.DirectionVertical));
            //鱼尾
            Rectangle rc2 = new Rectangle(20, 260, 80, 80);
            Rectangle rc3 = new Rectangle(-5, 265, 70, 70);
            g.DrawArc(Bp, rc2, 120, -240);
            g.DrawArc(Bp, rc3, 75, -150);

            double A_Branch_End_X = x1 - 60;
            double A_Branch_End_Y = y1;
            double A_Branch_Start_X = x1 - 150;
            double A_Branch_Start_Y = y1 + (y1 / 3) * 2;

            DataTable dt1 = new DataTable();
            if (dt.Rows.Count > 0) dt1 = (from r in dt.AsEnumerable() where r.Field<int>("Level") == 1 select r).CopyToDataTable<DataRow>();
            for (int i = 1; i < dt1.Rows.Count + 1; i++)
            {
                DataTable dt2 = (from r in dt.AsEnumerable() where r.Field<int>("Level") == 2 && r.Field<int>("ParentId") == DataConvert.StrToInt(dt1.Rows[i - 1]["Path"]) select r).CopyToDataTable<DataRow>();

                //一级支线
                if (i % 2 == 0)
                {
                    A_Branch_Start_Y = 0;
                    g.DrawLine(Sp, (float)A_Branch_End_X, (float)A_Branch_End_Y, (float)A_Branch_Start_X - 43, (float)A_Branch_Start_Y);
                    g.DrawString(dt1.Rows[i - 1]["Reason"].ToString(), Bfont, brush, (float)A_Branch_Start_X - 30, (float)A_Branch_Start_Y);

                }
                else
                {
                    A_Branch_Start_Y = 600;
                    g.DrawLine(Sp, (float)A_Branch_End_X, (float)A_Branch_End_Y, (float)A_Branch_Start_X - 43, (float)A_Branch_Start_Y);
                    g.DrawString(dt1.Rows[i - 1]["Reason"].ToString(), Bfont, brush, (float)A_Branch_Start_X - 50, (float)A_Branch_Start_Y);
                }


                if (i % 2 == 0)
                {
                    for (int j = 1; j < dt2.Rows.Count + 1; j++)
                    {
                        if (j % 2 == 0)
                        {
                            DataTable dt3 = new DataTable();
                            var d = (from r in dt.AsEnumerable() where r.Field<int>("Level") == 3 && r.Field<int>("ParentId") == DataConvert.StrToInt(dt2.Rows[j - 1]["Path"].ToString().Split('#')[1]) select r);
                            if (d.Count() > 0)
                            {
                                dt3 = d.CopyToDataTable<DataRow>();
                            }

                            double B_Branch_End_X = A_Branch_End_X - (A_Branch_End_X - A_Branch_Start_X) / 4 * j - 3 * j;
                            double B_Branch_End_Y = 300 - 60 * j;
                            double B_Branch_Start_X = B_Branch_End_X + (60 * dt3.Rows.Count) + 20;
                            double B_Branch_Start_Y = 300 - 60 * j;
                            if (dt2.Rows[j - 1]["Reason"].ToString().Split('#')[1] != "")
                            {
                                g.DrawLine(Sp, (float)B_Branch_End_X, (float)B_Branch_End_Y, (float)B_Branch_Start_X, (float)B_Branch_Start_Y);
                                g.DrawString(dt2.Rows[j - 1]["Reason"].ToString().Split('#')[1], Tfont, brush, new RectangleF((float)B_Branch_Start_X, (float)B_Branch_Start_Y - 18, 120.0F, 50.0F), format1);
                            }

                            for (int k = 1; k < dt3.Rows.Count + 1; k++)
                            {
                                if (k % 2 == 0)
                                {
                                    //上、右、上
                                    double C_Branch_End_X = B_Branch_End_X + 50 * k;
                                    double C_Branch_End_Y = B_Branch_Start_Y;
                                    double C_Branch_Start_X = C_Branch_End_X - 15;
                                    double C_Branch_Start_Y = C_Branch_End_Y - 25;
                                    if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2] != "")
                                    {
                                        g.DrawLine(Sp, (float)C_Branch_End_X, (float)C_Branch_End_Y, (float)C_Branch_Start_X, (float)C_Branch_Start_Y);
                                        if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2].Length > 8)
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 40, (float)C_Branch_Start_Y - 30, 100.0F, 50.0F), format1);
                                        }
                                        else if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2].Length < 4)
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 20, (float)C_Branch_Start_Y - 10, 100.0F, 20.0F), format1);
                                        }
                                        else
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 40, (float)C_Branch_Start_Y - 10, 100.0F, 20.0F), format1);
                                        }
                                    }
                                }
                                else
                                {
                                    //上、右、下
                                    double C_Branch_End_X = B_Branch_End_X + 50 * k;
                                    double C_Branch_End_Y = B_Branch_Start_Y;
                                    double C_Branch_Start_X = C_Branch_End_X - 15;
                                    double C_Branch_Start_Y = C_Branch_End_Y + 25;
                                    if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2] != "")
                                    {
                                        g.DrawLine(Sp, (float)C_Branch_End_X, (float)C_Branch_End_Y, (float)C_Branch_Start_X, (float)C_Branch_Start_Y);
                                        if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2].Length <= 6)
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 20, (float)C_Branch_Start_Y, 100.0F, 20.0F), format1);
                                        }
                                        else
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 40, (float)C_Branch_Start_Y, 100.0F, 50.0F), format1);
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            DataTable dt3 = new DataTable();
                            var d = (from r in dt.AsEnumerable() where r.Field<int>("Level") == 3 && r.Field<int>("ParentId") == DataConvert.StrToInt(dt2.Rows[j - 1]["Path"].ToString().Split('#')[1]) select r);
                            if (d.Count() > 0)
                            {
                                dt3 = d.CopyToDataTable<DataRow>();
                            }

                            double B_Branch_End_X = A_Branch_End_X - (A_Branch_End_X - A_Branch_Start_X) / 4 * j - 3 * j;
                            double B_Branch_End_Y = 300 - 60 * j;
                            double B_Branch_Start_X = B_Branch_End_X - (60 * dt3.Rows.Count) - 20;
                            double B_Branch_Start_Y = 300 - 60 * j;
                            if (dt2.Rows[j - 1]["Reason"].ToString().Split('#')[1] != "")
                            {
                                g.DrawLine(Sp, (float)B_Branch_End_X, (float)B_Branch_End_Y, (float)B_Branch_Start_X, (float)B_Branch_Start_Y);
                                g.DrawString(dt2.Rows[j - 1]["Reason"].ToString().Split('#')[1], Tfont, brush, new RectangleF((float)B_Branch_Start_X - 70, (float)B_Branch_Start_Y - 18, 120.0F, 50.0F), format1);
                            }

                            for (int k = 1; k < dt3.Rows.Count + 1; k++)
                            {
                                if (k % 2 == 0)
                                {
                                    //上、左、上
                                    double C_Branch_End_X = B_Branch_End_X - 50 * k;
                                    double C_Branch_End_Y = B_Branch_Start_Y;
                                    double C_Branch_Start_X = C_Branch_End_X - 15;
                                    double C_Branch_Start_Y = C_Branch_End_Y + 25;
                                    if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2] != "")
                                    {
                                        g.DrawLine(Sp, (float)C_Branch_End_X, (float)C_Branch_End_Y, (float)C_Branch_Start_X, (float)C_Branch_Start_Y);
                                        if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2].Length <= 6)
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 20, (float)C_Branch_Start_Y, 100.0F, 20.0F), format1);
                                        }
                                        else
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 40, (float)C_Branch_Start_Y, 100.0F, 50.0F), format1);
                                        }
                                    }
                                }
                                else
                                {
                                    //上、左、上
                                    double C_Branch_End_X = B_Branch_End_X - 50 * k;
                                    double C_Branch_End_Y = B_Branch_Start_Y;
                                    double C_Branch_Start_X = C_Branch_End_X - 15;
                                    double C_Branch_Start_Y = C_Branch_End_Y - 25;
                                    if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2] != "")
                                    {
                                        g.DrawLine(Sp, (float)C_Branch_End_X, (float)C_Branch_End_Y, (float)C_Branch_Start_X, (float)C_Branch_Start_Y);
                                        if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2].Length > 8)
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 50, (float)C_Branch_Start_Y - 30, 100.0F, 50.0F), format1);
                                        }
                                        else if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2].Length < 4)
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 20, (float)C_Branch_Start_Y - 20, 100.0F, 20.0F), format1);
                                        }
                                        else
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 50, (float)C_Branch_Start_Y - 20, 100.0F, 20.0F), format1);
                                        }
                                    }
                                }
                            }
                        }
                    }

                }
                else
                {
                    for (int j = 1; j < dt2.Rows.Count + 1; j++)
                    {
                        if (j % 2 == 0)
                        {
                            DataTable dt3 = new DataTable();
                            var d = (from r in dt.AsEnumerable() where r.Field<int>("Level") == 3 && r.Field<int>("ParentId") == DataConvert.StrToInt(dt2.Rows[j - 1]["Path"].ToString().Split('#')[1]) select r);
                            if (d.Count() > 0)
                            {
                                dt3 = d.CopyToDataTable<DataRow>();
                            }

                            double B_Branch_End_X = A_Branch_End_X - (A_Branch_End_X - A_Branch_Start_X) / 4 * j - 3 * j;
                            double B_Branch_End_Y = 300 + 60 * j;
                            double B_Branch_Start_X = B_Branch_End_X + (60 * dt3.Rows.Count) + 20;
                            double B_Branch_Start_Y = 300 + 60 * j;
                            if (dt2.Rows[j - 1]["Reason"].ToString().Split('#')[1] != "")
                            {
                                g.DrawLine(Sp, (float)B_Branch_End_X, (float)B_Branch_End_Y, (float)B_Branch_Start_X, (float)B_Branch_Start_Y);
                                g.DrawString(dt2.Rows[j - 1]["Reason"].ToString().Split('#')[1], Tfont, brush, new RectangleF((float)B_Branch_Start_X, (float)B_Branch_Start_Y - 18, 120.0F, 50.0F), format1);
                            }

                            for (int k = 1; k < dt3.Rows.Count + 1; k++)
                            {
                                if (k % 2 == 0)
                                {
                                    //下、右、上
                                    double C_Branch_End_X = B_Branch_End_X + 50 * k;
                                    double C_Branch_End_Y = B_Branch_Start_Y;
                                    double C_Branch_Start_X = C_Branch_End_X - 15;
                                    double C_Branch_Start_Y = C_Branch_End_Y - 25;
                                    if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2] != "")
                                    {
                                        g.DrawLine(Sp, (float)C_Branch_End_X, (float)C_Branch_End_Y, (float)C_Branch_Start_X, (float)C_Branch_Start_Y);
                                        if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2].Length > 8)
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 40, (float)C_Branch_Start_Y - 30, 100.0F, 50.0F), format1);
                                        }
                                        else if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2].Length < 4)
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 20, (float)C_Branch_Start_Y - 10, 100.0F, 20.0F), format1);
                                        }
                                        else
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 40, (float)C_Branch_Start_Y - 10, 100.0F, 20.0F), format1);
                                        }
                                    }
                                }
                                else
                                {
                                    //下、右、下
                                    double C_Branch_End_X = B_Branch_End_X + 50 * k;
                                    double C_Branch_End_Y = B_Branch_Start_Y;
                                    double C_Branch_Start_X = C_Branch_End_X - 15;
                                    double C_Branch_Start_Y = C_Branch_End_Y + 25;
                                    if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2] != "")
                                    {
                                        g.DrawLine(Sp, (float)C_Branch_End_X, (float)C_Branch_End_Y, (float)C_Branch_Start_X, (float)C_Branch_Start_Y);
                                        if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2].Length <= 6)
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 20, (float)C_Branch_Start_Y, 100.0F, 20.0F), format1);
                                        }
                                        else
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 40, (float)C_Branch_Start_Y, 100.0F, 50.0F), format1);
                                        }

                                    }
                                }
                            }
                        }
                        else
                        {
                            DataTable dt3 = new DataTable();
                            var d = (from r in dt.AsEnumerable() where r.Field<int>("Level") == 3 && r.Field<int>("ParentId") == DataConvert.StrToInt(dt2.Rows[j - 1]["Path"].ToString().Split('#')[1]) select r);
                            if (d.Count() > 0)
                            {
                                dt3 = d.CopyToDataTable<DataRow>();
                            }

                            double B_Branch_End_X = A_Branch_End_X - (A_Branch_End_X - A_Branch_Start_X) / 4 * j - 3 * j;
                            double B_Branch_End_Y = 300 + 60 * j;
                            double B_Branch_Start_X = B_Branch_End_X - (60 * dt3.Rows.Count) - 20;
                            double B_Branch_Start_Y = 300 + 60 * j;

                            if (dt2.Rows[j - 1]["Reason"].ToString().Split('#')[1] != "")
                            {
                                g.DrawLine(Sp, (float)B_Branch_End_X, (float)B_Branch_End_Y, (float)B_Branch_Start_X, (float)B_Branch_Start_Y);
                                g.DrawString(dt2.Rows[j - 1]["Reason"].ToString().Split('#')[1], Tfont, brush, new RectangleF((float)B_Branch_Start_X - 70, (float)B_Branch_Start_Y - 18, 120.0F, 50.0F), format1);
                            }

                            for (int k = 1; k < dt3.Rows.Count + 1; k++)
                            {
                                if (k % 2 == 0)
                                {
                                    //下、左、下
                                    double C_Branch_End_X = B_Branch_End_X - 50 * k;
                                    double C_Branch_End_Y = B_Branch_Start_Y;
                                    double C_Branch_Start_X = C_Branch_End_X - 15;
                                    double C_Branch_Start_Y = C_Branch_End_Y + 25;
                                    if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2] != "")
                                    {
                                        g.DrawLine(Sp, (float)C_Branch_End_X, (float)C_Branch_End_Y, (float)C_Branch_Start_X, (float)C_Branch_Start_Y);
                                        if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2].Length <= 6)
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 20, (float)C_Branch_Start_Y, 100.0F, 30.0F), format1);
                                        }
                                        else
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 40, (float)C_Branch_Start_Y, 100.0F, 50.0F), format1);
                                        }
                                    }

                                }
                                else
                                {
                                    //下、左、上
                                    double C_Branch_End_X = B_Branch_End_X - 50 * k;
                                    double C_Branch_End_Y = B_Branch_Start_Y;
                                    double C_Branch_Start_X = C_Branch_End_X - 15;
                                    double C_Branch_Start_Y = C_Branch_End_Y - 25;
                                    if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2] != "")
                                    {
                                        g.DrawLine(Sp, (float)C_Branch_End_X, (float)C_Branch_End_Y, (float)C_Branch_Start_X, (float)C_Branch_Start_Y);
                                        if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2].Length > 6)
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 40, (float)C_Branch_Start_Y - 30, 100.0F, 50.0F), format1);
                                        }
                                        else if (dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2].Length < 4)
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 20, (float)C_Branch_Start_Y - 20, 100.0F, 20.0F), format1);
                                        }
                                        else
                                        {
                                            g.DrawString(dt3.Rows[k - 1]["Reason"].ToString().Split('#')[2], font, brush, new RectangleF((float)C_Branch_Start_X - 40, (float)C_Branch_Start_Y - 20, 100.0F, 20.0F), format1);
                                        }
                                    }
                                }
                            }
                        }

                    }
                }

                A_Branch_End_X = A_Branch_End_X - 200;
                A_Branch_Start_X = A_Branch_Start_X - 200;
            }

            MemoryStream stream = new MemoryStream();//保存绘制的图片
            img.Save(stream, ImageFormat.Jpeg);//保存绘制的图片
            return stream;
        }

        #endregion
    }
}